<template>
    <div v-loading="loading">
        <el-form ref="formRef" :model="formData" label-width="100px">
            <el-card shadow="never" class="!border-none">
                <el-form-item label="地图key：" prop="key">
                    <div class="w-80">
                        <el-input v-model.trim="formData.key" placeholder="请输入地图key" />
                        <div class="el-form-tips">仅支持高德地图</div>
                    </div>
                </el-form-item>
                <el-form-item label="js安全秘钥：" prop="code">
                    <div class="w-80">
                        <el-input v-model.trim="formData.code" placeholder="请输入js安全秘钥" />
                        <div class="el-form-tips">请前往高德开放平台申请</div>
                    </div>
                </el-form-item>
            </el-card>
        </el-form>
        <footerSubmit>
			<el-button v-auth="'setting/setMapInfo'" type="primary" @click="handleSubmit">保存</el-button>
		</footerSubmit>
    </div>
</template>

<script lang="ts" setup name="systemMap">
import { ref, reactive, onMounted } from 'vue';
import { getMapInfo, setMapInfo } from '/@/api/setting/webset';
import { messageSuccess } from '/@/utils/message';

const formRef = ref(null);

const loading = ref(true);

// 表单数据
const formData = reactive({
    key: '', // 地图key
    code: '', // js安全秘钥
});

// 获取数据
const getData = async () => {
    const data = await getMapInfo();
    for (const key in formData) {
		if(data[key] != null && data[key] != undefined) {
			formData[key] = data[key];
		}
    }
    loading.value = false;
};

// 保存提交
const handleSubmit = async () => {
    await formRef.value?.validate();
    await setMapInfo(formData);
    messageSuccess('设置成功');
    getData();
};

onMounted(() => {
	getData();
});
</script>